package com.hjimi.ski.presenter;

import android.app.Activity;
import android.provider.Settings;
import android.util.Log;

import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.UpdateIpIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.DiscoverNetIpUtil;
import com.hjimi.ski.utils.ToastUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created By 孙岩
 * on 2023/7/14
 */
public class UpdateIpPresenter extends BasePresenter<UpdateIpIView>{
    @SuppressWarnings("unchecked")
    public void updateIp(String deviceId,Activity activity){
        if(isViewAttached()){
            String ip = DiscoverNetIpUtil.getIpAddress(activity);
            String url = UrlConfig.updateIp + deviceId + "/"+ip;
            DataModel.request(ModelToken.GET_REQUEST)
                    .url(url)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    getView().showUpdateIpResult();
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {

                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
